
package com.android.matmanual;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;

import android.os.Bundle;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
 * @author zhangyu
 * 20120328
 */
public class FlashLightTest extends Activity implements OnClickListener{

    private static Camera mCamera = null;
    private static Parameters mCameraParas = null;
    public static final byte[] LIGHTE_ON = { '2', '5', '5' };
    public static final byte[] LIGHTE_OFF = { '0' };
    
	WakeLock mWakeLock;
	private Button cameraSuccess, cameraFailure;
	private EngSqlite mEngSqlite;
	private String testCaseName;
  private int groupId;
  	
  TextView flashlight_info;
  
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		/*setTitle(getResources().getString(R.string.checkcameratitle));*/
		setTitle("闪关灯测试");
		/*requestWindowFeature(Window.FEATURE_NO_TITLE);
	    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);*/
		setContentView(R.layout.flashlight);
		PowerManager pm =(PowerManager) getSystemService(POWER_SERVICE); 
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,"XYTEST"); 
		mWakeLock.acquire(); 
		setLEDStatus(true);
		
		flashlight_info = (TextView)findViewById(R.id.flashlight_info);
		flashlight_info.setText("闪关灯已经打开请检查");
		
		
        groupId = this.getIntent().getIntExtra("groupId", 0);
        testCaseName = this.getIntent().getStringExtra("testname");
        cameraSuccess = (Button) findViewById(R.id.btn_pass);
        cameraFailure = (Button) findViewById(R.id.btn_fail);
        mEngSqlite = EngSqlite.getInstance(this);
        cameraSuccess.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	mEngSqlite.updateDB(groupId,testCaseName, Const.SUCCESS);
            	finish();
            }
        });
        cameraFailure.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
            	mEngSqlite.updateDB(groupId,testCaseName, Const.FAIL);
            	finish();
            }
        });
        
	}
	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
		 	

		 	if(keyCode == KeyEvent.KEYCODE_BACK)
		 	{
		 		setLEDStatus(false);
				finish();
		 	}	 
		 	return true;
	 	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
        if(null != mCamera){
            mCamera.release();
            mCamera = null;
        }

	}

	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
        finish();
		super.onStop();
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
    private void setLEDStatus(boolean status) {
        FileOutputStream red;
        Log.v("leiyonghui", "status:"+status);
        try {

            byte[] ledData = status ? LIGHTE_ON : LIGHTE_OFF;
            red = new FileOutputStream("/sys/class/leds/flashlight/brightness");
            red.write(ledData);
            red.close();
        } catch (FileNotFoundException e) {

        	Log.v("leiyonghui", "FileNotFoundException:"+status);
            if(null == mCamera){
                mCamera = Camera.open(0);
            }

            mCameraParas = mCamera.getParameters();
            if(status){
                mCameraParas.setFlashMode(Parameters.FLASH_MODE_TORCH);
                mCamera.setParameters(mCameraParas);
            }else{
                mCameraParas.setFlashMode(Parameters.FLASH_MODE_OFF);
                mCamera.setParameters(mCameraParas);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
		 